package com.itsq.service.resources.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.mapper.CarMapper;
import com.itsq.pojo.dto.PageViolationDto;
import com.itsq.pojo.entity.Car;
import com.itsq.pojo.entity.Violation;
import com.itsq.mapper.ViolationMapper;
import com.itsq.service.resources.ViolationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsq.utils.wx.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-02-01
 */
@Service
public class ViolationServiceImpl extends ServiceImpl<ViolationMapper, Violation> implements ViolationService {

    @Autowired(required = false)
    private ViolationMapper violationMapper;
    @Autowired(required = false)
    private CarMapper carMapper;

    @Override
    public Page<Violation> selViolationPageFront(PageViolationDto pageViolationDto) {
        Page<Violation> page=new Page<>(pageViolationDto.getPageIndex(),pageViolationDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageViolationDto.getOrderByField()==null){
            queryWrapper.orderByDesc("time");
        }else{
            queryWrapper.orderByDesc(pageViolationDto.getOrderByField());
        }
        if(pageViolationDto.getCarNumber()!=null){
            queryWrapper.like("car_number",pageViolationDto.getCarNumber());
        }
        if(pageViolationDto.getCity()!=null){
            queryWrapper.eq("city",pageViolationDto.getCity());
        }
        return violationMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<Violation> selViolationPageAfter(PageViolationDto pageViolationDto) {
        Page<Violation> page=new Page<>(pageViolationDto.getPageIndex(),pageViolationDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageViolationDto.getOrderByField()==null){
            queryWrapper.orderByDesc("time");
        }else{
            queryWrapper.orderByDesc(pageViolationDto.getOrderByField());
        }
        if(pageViolationDto.getCarNumber()!=null){
            queryWrapper.like("car_number",pageViolationDto.getCarNumber());
        }
        if(pageViolationDto.getCity()!=null){
            queryWrapper.eq("city",pageViolationDto.getCity());
        }
        return violationMapper.selectPage(page,queryWrapper);
    }

    @Override
    public String findwz(Integer id) {
        //查询对应车信息
        Car car = carMapper.selectById(id);
        JSONObject json=new JSONObject();
        //车类型
        json.put("carType","02");
        //车牌号
        json.put("carNumber",car.getCarNumber());
        //车架号后6位
        json.put("carCode",car.getFrame().length() - 6);
        //发动机号后6位
        json.put("carEngineCode",car.getEngine().length() - 6);
        String url="https://route.showapi.com/862-1?showapi_appid=532727&&?showapi_sign=af95352e360d4606bf613fe55b6e0ef5";
        String s = HttpClientUtil.doPostJson(url, json.toString());
        if(s!=null&&!s.equals("")){
            //创建违章信息表对象
            JSONObject jsonObject =JSONObject.parseObject(s);
            //数据data转换成集合
            List<Map> records = JSONObject.parseArray(jsonObject.get("records").toString(), Map.class);
            Violation v=new Violation();
            //违章车牌号赋值
            v.setCarNumber(jsonObject.get("carNumber").toString());
            //循环可能存在的多条违章记录
            for (Map record : records) {
                //违章罚款赋值
                v.setFine(Integer.parseInt(record.get("money").toString()));
                //违章记分赋值
                v.setFraction(Integer.parseInt(record.get("degree").toString()));
                //违章时间赋值
                try {
                    v.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(record.get("time").toString()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //违章行为赋值
                v.setBehavior(record.get("reason").toString());
                //违章地址赋值
                v.setAddress(record.get("address").toString());
                //违章代码赋值
                v.setCode(record.get("code").toString());
                //违章城市赋值
                v.setCity(record.get("cityName").toString());
                //违章处理编号赋值
                v.setNumber(record.get("department").toString());
                //添加数据
                violationMapper.insert(v);
            }
        }
        return s;
    }

    @Override
    public Integer insert(Violation violation) {
        return violationMapper.insert(violation);
    }

    @Override
    public Integer update(Violation violation) {
        return violationMapper.updateById(violation);
    }

    @Override
    public Integer delete(Integer id) {
        return violationMapper.deleteById(id);
    }
}
